package com.service.impl.order;

import com.client.DadaApiResponse;
import com.config.AppConfig;
import com.request.OrderCallbackReq;
import com.service.OrderCallbackService;
import com.utils.DadaSignatureUtil;
import com.utils.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

/**
 * 订单回调
 * @author zhang
 */
@Slf4j
@Service
public class OrderCallbackServiceImpl implements OrderCallbackService {

    @Value("${spring.config.isOnline}")
    private Boolean IS_ONLINE;

    /**
     * 订单回调
     * @param orderCallbackReq
     * @return DadaApiResponse
     */
    @Override
    public DadaApiResponse orderCallback(OrderCallbackReq orderCallbackReq) {
        DadaApiResponse dadaApiResponse = JSONUtil.fromJson(getCallbackRequestParams(orderCallbackReq), DadaApiResponse.class);
        return dadaApiResponse;
    }

    private String getCallbackRequestParams(OrderCallbackReq orderCallbackReq) {
        //初始化配置(isOnline表示是否测试环境)
        AppConfig appConfig = new AppConfig(IS_ONLINE);
        appConfig.setOnline(IS_ONLINE);

        Map<String, String> requestParams = new HashMap<String, String>(8);
        requestParams.put("client_id", orderCallbackReq.getClientId());
        requestParams.put("order_id", orderCallbackReq.getOrderId());
        requestParams.put("update_time", String.valueOf(orderCallbackReq.getUpdateTime()));
        requestParams.put("signature", DadaSignatureUtil.getSign(requestParams,appConfig));
        return JSONUtil.toJson(requestParams);
    }

}
